package com.xk._02算法篇._11sequence;

/**
 * @description: 蛮力算法2
 * @author: xu
 * @date: 2022/10/23 22:02
 */
public class BruteForce03 {
    // 时间：O(n * m)
    // 空间：O(n + m)
    public static int indexOf(String test, String pattern) {
        if (test == null || pattern == null) return -1;
        char[] testChars = test.toCharArray();
        char[] patternChars = pattern.toCharArray();
        int tlen = testChars.length, plen = patternChars.length;
        if (tlen == 0 || plen == 0 || tlen < plen) return -1;

        int tiMax = tlen - plen;
        for (int ti = 0; ti <= tiMax; ti++){
            int pi = 0;
            for ( ; pi < plen; pi++) {
                if (testChars[ti + pi] != patternChars[pi]) break;
            }
            if (pi == plen) return ti;
        }
        return -1;
    }
}
